package com.csun.nusing.ui.activity;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.csun.nusing.R;
import com.csun.nusing.bean.UrlBean;
import com.csun.nusing.bean.jsonbean.FollowDeviceSearchEcgJsonBean;
import com.csun.nusing.bean.jsonbean.FollowDeviceSearchEcgJsonBean.FollowDeviceSearchEcgDataBean;
import com.csun.nusing.bean.jsonbean.FollowDeviceSearchEcgJsonBean.FollowDeviceSearchEcgDataBean.FollowDeviceSearchEcgListBean;
import com.csun.nusing.presenter.CommonPresenter;
import com.csun.nusing.presenter.inter.IBase;
import com.csun.nusing.ui.BaseActivity;
import com.csun.nusing.ui.adapter.MyBaseAdapter;
import com.csun.nusing.ui.adapter.ViewHolder;
import com.csun.nusing.util.SPUtils;
import com.google.gson.Gson;
import com.home.administrator.okhttplibrary.IOkTaskCallback;
import com.home.administrator.okhttplibrary.OkHttpTask;
import com.home.administrator.okhttplibrary.OkHttpTask.OkHttpTaskBuidler.OkType;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 血压测试的主界面 ClassName: BloodPressTestActivity
 * 
 * @Description: TODO
 * @author fzq
 * @date 2017-10-25
 */
public class ECGHistoryActivity extends BaseActivity<CommonPresenter> implements
		IBase {

	private RelativeLayout navrl;
	private boolean isFamily;
	private ListView lv;
	private String olderId;
	private String authorization;
	protected FollowDeviceSearchEcgJsonBean ecgdataJsonBean;
	protected Intent intent;
	protected TextView timetv;
	protected TextView statetv;
	protected SimpleDateFormat sdf;

	@Override
	public int getContentViewId() {
		return R.layout.activity_ecg_history;
	}

	@Override
	public void initSubViews(View view) {
		super.initSubViews(view);
		findView();
	}

	/**
	 * 初始化控件
	 * 
	 * @Description: TODO
	 * @param
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-10-26
	 */
	private void findView() {
		lv = findViewById(R.id.ecg_history_date_lv);
	}

	@Override
	public void initData() {
		super.initData();
		navrl = findViewById(R.id.nav_rl);
		isFamily = getIntent().getBooleanExtra("isFamily", false);
		olderId = getIntent().getStringExtra("olderId");
		if (isFamily) {
			navrl.setBackgroundResource(R.drawable.main_whiteyello_color_shape);
		}

		requestData();
	}

	private void requestData() {
		authorization = (String) SPUtils.get(this, "authorization", "");
		Map<String, String> param = new HashMap<String, String>();
		param.put("id", "" + olderId);
		param.put("pageNo", "1");
		param.put("pageSize", "1000");
		OkHttpTask.newInstance(this).post(param, OkType.form)
				.addHeader("authorization", authorization)
				.enqueue(new IOkTaskCallback() {
					@Override
					public void onSuccess(String result) {
						if (result != null) {
							Log.e("xindian", "----------***  " + result);
							Gson gson = new Gson();
							ecgdataJsonBean = gson.fromJson(result,
									FollowDeviceSearchEcgJsonBean.class);
							if (ecgdataJsonBean != null
									&& ecgdataJsonBean.getData() != null) {
								controlxindianData(ecgdataJsonBean.getData());
							}
						}
					}
				}).start(UrlBean.SEARCHHISTORYECGRELATIONURL);
	}

	protected void controlxindianData(final FollowDeviceSearchEcgDataBean data) {
		if (data.getList() != null) {
			MyBaseAdapter<FollowDeviceSearchEcgListBean> adapter = new MyBaseAdapter<FollowDeviceSearchEcgListBean>(
					this, data.getList(), R.layout.item_ecghistory_ll) {
				@Override
				public void convert(ViewHolder helper, int position,
						FollowDeviceSearchEcgListBean item) {
					timetv = helper.getView(R.id.item_history_ll_time_tv);
					statetv = helper.getView(R.id.item_history_ll_state_tv);
					if (item != null) {
						sdf = new SimpleDateFormat("MM月dd日 HH时");
						Date date = new Date(item.getModifyTime());
						timetv.setText("" + sdf.format(date));
					}
				}
			};
			lv.setAdapter(adapter);
			lv.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					if (data.getList().get(position)!=null) {
						Intent intent = new Intent(ECGHistoryActivity.this,
								EcgHistoryitemActivity.class);
						intent.putExtra("data", ""
								+ data.getList().get(position).getEcg());
						intent.putExtra("isFamily",isFamily);
						startActivity(intent);
					}

				}
			});

		}
	}

}
